package com.hotkid.ceo.jetpack.myjetpackmvvm.base

import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding
import com.hotkid.ceo.jetpack.myjetpackmvvm.common.MyConfig
import com.hotkid.ceo.jetpack.myjetpackmvvm.ui.login.LoginActivity
import com.yechaoa.yutilskt.SpUtil

/**
 * ClassName:BaseVmActivity
 * Package:com.hotkid.ceo.jetpack.myjetpackmvvm.base
 * Description:
 * @date:2021/3/23 7:58 PM
 * @author:774169396@qq.com
 */
abstract class BaseVmActivity<VB:ViewBinding,VM:BaseViewModel> :BaseActivity<VB>(){
    protected open lateinit var mViewModel: VM

    //分页的参数
    protected open val mTotalCount=20//每次加载的数量
    protected  open var mCurrentSize=0//当前加载的数量
    protected open var mCurrentPage=0//前前加载的页数

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initViewModel()
        observe()
        initView()
        initData()
        setListeneres()
    }

    /**
     * 设置监听回调的地方
     */
    open fun setListeneres() {

    }

    /**
     * 初始化data相关
     */
    open fun initData() {}
    /**
     * 初始化view相关
     */
    open fun initView(){}

    /**
     * 订阅，有逻辑的话，复写的时候super不要去掉
     */
    open fun observe() {
        mViewModel.needLogin.observe(this, {
            if (it) {
                SpUtil.setBoolean(MyConfig.IS_LOGIN, false)
                startActivity(Intent(this,LoginActivity::class.java))
            }
        })
    }

    /**
     * 初始化VeiwModel
     */
    private fun initViewModel() {
        mViewModel=ViewModelProvider(this).get(veiwModelClass())
    }

    /**
     * 获取ViewModel的class
     */
    abstract fun veiwModelClass(): Class<VM>
}